package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exception.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author shaki
 * @since 2021-06-22
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public String login(UcenterMember member) {
        String email = member.getEmail();
        String password = member.getPassword();
        if(StringUtils.isNullOrEmpty(email) || StringUtils.isNullOrEmpty(password)){
            throw new MyException(20001,"登录失败！");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember == null){
            throw new MyException(20001,"邮箱不存在！");
        }
        //把输入的密码先加密再与数据库对比
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new MyException(20001,"密码错误！");
        }
        if(ucenterMember.getIsDisabled()){
            throw new MyException(20001,"登录失败，该用户被禁用！");
        }
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        String email = registerVo.getEmail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if(StringUtils.isNullOrEmpty(code) || StringUtils.isNullOrEmpty(email)
        || StringUtils.isNullOrEmpty(nickname) || StringUtils.isNullOrEmpty(password)){
            throw new MyException(20001,"注册失败！");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(email).toString();
        if(!code.equals(redisCode)){
            throw new MyException(20001,"验证码无效！");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new MyException(20001,"注册失败，用户已存在！");
        }
        UcenterMember member = new UcenterMember();
        member.setEmail(email);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid){
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
